/**
 * 属于mainpage.js的一部分
 */


//TODO 显示错误信息，后期抽象出一个公共函数
function showErrMsg(id,errMsg){
	$("#"+id).text(errMsg);
	$("#"+id).css("color","red");
	$("#"+id).css("visibility","visible");
	$("#"+id).fadeIn(150);
}

function hideErrMsg(id){
	$("#"+id).css("visibility","hidden");
}

function initSetWave(){
	
	//向后台取对应波长的阈值
	$("#waveVal").blur(function(){
		var wave = $(this).val();
		
		if(validateWave(wave)){
			$.ajax({
		        type: "post",
		        url: $('#basePath').val()+"getWaveThreshold",
		        data: {
		        	"waveVal":wave
		        },
		        success:function(data,status) {
		        	
		        	if(!isNaN(data.threshold)){
		        		//设置保留5位小数
		        		threshold = new Number(data.threshold);
		        		$("#threshold").val(threshold.toFixed(5));
		        	}else{
		        		alert("ajax error");
		        	}
		        	
		        },
		        error:function(data,status){
		        	alert("ajax error.");
		        }
		     });
		}
	});
	
	//修改wave的报警阈值
	$("#submitWave").click(function(){
		var wave = $("#waveVal").val();
		var threshold = $("#threshold").val();
		
		if(!validateWave(wave)){
			return;
		}
		if(wave==""){
			showErrMsg("waveErrSpan","填写波长");
			return;
		}
		if(!validateThreshold(threshold)){
			return;
		}
		
		$.ajax({
	        type: "post",
	        url: $('#basePath').val()+"setWaveThreshold",
	        data: {
	        	"waveVal":wave,
	        	"threshold":threshold
	        },
	        success:function(data,status) {
	        	if(!data.success){
	        		alert("修改错误，请稍后重试");
	        	}else{
	        		$("#waveErrSpan").fadeIn();
	        		$("#waveErrSpan").text("修改成功");
	        		$("#waveErrSpan").css("color","green");
	        		$("#waveErrSpan").css("visibility","visible");
	        		$("#waveErrSpan").fadeOut(1500);
	        	}
	        },
	        error:function(data,status){
	        	alert("ajax error.");
	        }
	     });
		
	});
}

/**验证波长阈值填写信息*/
function validateThreshold(threshold){
	if(threshold==""){
		showErrMsg("waveErrSpan","填写阈值");
		return false;
	}
	if(isNaN(threshold)){
		showErrMsg("waveErrSpan","格式错误");
		return false;
	}
	hideErrMsg("waveErrSpan");
	return true;
}

/**验证波长填写信息，在blur以及提交时触发*/
function validateWave(wave){
	
	//不为空
	if(wave==""){
		hideErrMsg("waveErrSpan");
		return false;
	}
	
	//波长范围确定，要求为偶数
	if(wave%2!=0 || wave>710 || wave<200){
		showErrMsg("waveErrSpan","200-708偶数");
		return false;
	}
	
	//检查是否是数字
	if(isNaN(wave)){
		showErrMsg("waveErrSpan","格式错误");
		return false;
	}
	hideErrMsg("waveErrSpan");
	return true;
}
